using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using InfoSoftGlobal;
using System.Data.SqlClient;

public partial class TrendChart : System.Web.UI.Page
{
    SQLInteract objSQL = new SQLInteract();

    protected void Page_Load(object sender, EventArgs e)
    {
        fillSystemdropdown();
    }
    protected void ddSystem_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddRelease.Items.Clear();
        ListItem li = new ListItem("Choose");
        li.Value = "-1";
        ddRelease.Items.Add(li);
        divProject.Visible = false;
        btnChart.Visible = false;
        fcLiteral.Visible = false;
        fillReleasedropdown();
    }

    public void fillSystemdropdown()
    {
        DataSet dsSystem = new DataSet();
        try
        {
            dsSystem = objSQL.ReturnDataAdapter("select distinct System_Detected_In from Defects order by System_Detected_In desc");
            ddSystem.DataSource = dsSystem;
            ddSystem.DataTextField = "System_Detected_In";
            ddSystem.DataValueField = "System_Detected_In";
            ddSystem.DataBind();
        }
        catch (Exception ex)
        {
            this.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alert", "<script language=\"javascript\" type=\"text/javascript\" >alert(\"Error: " + ex.Message + "\");</script>", false);
        }
        finally
        {
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        fcLiteral.Visible = false;
        btnChart.Visible = true;
        divProject.Visible = true;
    }

    public void fillReleasedropdown()
    {
        DataSet dsRelease = new DataSet();
        try
        {
            dsRelease = objSQL.ReturnDataAdapter("select distinct Detected_In_Release from Defects where System_Detected_In='" + ddSystem.SelectedValue.ToString().Trim() + "' order by Detected_In_Release desc");
            ddRelease.DataSource = dsRelease;
            ddRelease.DataTextField = "Detected_In_Release";
            ddRelease.DataValueField = "Detected_In_Release";
            ddRelease.DataBind();
        }
        catch (Exception ex)
        {
            this.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alert", "<script language=\"javascript\" type=\"text/javascript\" >alert(\"Error: " + ex.Message + "\");</script>", false);
        }
        finally
        {
        }
    }
    protected void btnChart_Click(object sender, EventArgs e)
    {
        string cycle = rbnCycle.SelectedValue.ToString().Trim();
        string release_name = ddRelease.SelectedValue.ToString().Trim();
        SqlDataReader sdr,sdr1=null;
        Array statusClosedarray;
        Array statusReopenarray;
        ArrayList arrDates=new ArrayList();

        int statusClosed = 0;
        string statusClosedList = "";

        int statusReopen = 0;
        string statusReopenList = "";
        sdr = objSQL.ReturnDataReader("select distinct(Detected_on_Date_From) from Defects where System_Detected_In='" + ddSystem.SelectedValue.ToString().Trim() + "' and Detected_In_Release like '%" + release_name + "%' and  Detected_In_Cycle like '%" + rbnCycle.SelectedValue.ToString().Trim() + "%'  order by Detected_on_Date_From");
        if (sdr.HasRows)
        {
            while (sdr.Read())
            {
                arrDates.Add(DateTime.Parse(sdr["Detected_on_Date_From"].ToString()).ToString("MM/dd/yyyy"));
            }
        }

        foreach (string item in arrDates)
        {
            if (item != "")
            {
                statusClosed = 0;
                statusReopen = 0;
                sdr1 = objSQL.ReturnDataReader("select * from Defects where System_Detected_In='" + ddSystem.SelectedValue.ToString().Trim() + "' and Detected_on_Date_From <= '" + item + "' and  Detected_In_Release like '%" + release_name + "%' and  Detected_In_Cycle like '%" + rbnCycle.SelectedValue.ToString().Trim() + "%'  order by Detected_on_Date_From");
                if (sdr1.HasRows)
                {
                    while (sdr1.Read())
                    {
                        if (sdr1["Status"].ToString().Trim() == "Closed")
                        {
                            statusClosed = statusClosed + 1;
                        }
                        else
                        {
                            statusReopen = statusReopen + 1;
                        }
                    }
                    statusClosedList = statusClosedList + statusClosed.ToString().Trim() + ",";
                    statusReopenList = statusReopenList + statusReopen.ToString().Trim() + ",";
                }
            }
        }
        statusClosedarray = statusClosedList.Split(new char[] { ',' });
        statusReopenarray = statusReopenList.Split(new char[] { ',' });

        string strXML1 = GenerateXML(arrDates, statusClosedarray, statusReopenarray, release_name);
        fcLiteral.Text = FusionCharts.RenderChart("FusionCharts/FCF_MSLine.swf", "",strXML1, "myNext", "800", "500", false, false);
        fcLiteral.Visible = true;
    }

    public string GenerateXML(ArrayList date_list, Array statusClosedarray,Array statusReopenarray, string release_name)
    {
        string strXML = "";

        strXML = "<graph caption='Defect Status' subcaption='' hovercapbg='FFECAA' hovercapborder='F47E00' formatNumberScale='0' decimalPrecision='0' showvalues='0' numdivlines='10' numVdivlines='0' yaxisminvalue='100' yaxismaxvalue='100' rotateNames='1'>";

        //CATEGORIES
        strXML += "<categories>";
        foreach (string items in date_list)
        {
            if (items.ToString() != "")
            {
                strXML += "<category name='" + items.ToString().Trim() + "' />";
            }
        }
        strXML += "</categories>";

        //statusClosed
        strXML += "<dataset seriesName='Closed' color='F1683C' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'>";
        foreach (string items in statusClosedarray)
        {
            if (items != "")
            {
                strXML += "<set value='" + items + "' />";
            }
        }
        strXML += "</dataset>";

        //StatusReopen
        strXML += "<dataset seriesName='Open' color='2AD62A' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'>";
        foreach (string item in statusReopenarray)
        {
            if (item != "")
            {
                strXML += "<set value='" + item + "' />";
            }
        }
        strXML += "</dataset>";
        strXML += "</graph>";
        return strXML;
    }
}
